<template>
  <div>
    <el-container>
      <!-- 筛选条件-->
      <el-header>
        <el-row :gutter="20">

          <el-col :span="6">
            <el-input size="medium" placeholder="请输入角色名称" prefix-icon="el-icon-search" style="width: 70%;border-radius: 30px" v-model="filter.name" clearable></el-input>
          </el-col>

          <el-col :span="6">
            <el-date-picker v-model="filter.startTime" type="date" placeholder="开始日期"></el-date-picker>
          </el-col>

          <el-col :span="1"> - </el-col>

          <el-col :span="6">
            <el-date-picker v-model="filter.endTime" type="date" placeholder="结束日期"></el-date-picker>
          </el-col>

          <el-button type="success" @click="handleAdd()" round>添加</el-button>
          <el-button type="primary" @click="getData()" round>查询</el-button>

        </el-row>
      </el-header>
      <!-- 主表-->
      <el-main>
        <el-table :data=tableData style="width: 100%">
          <el-table-column label="角色主键" prop="id" v-if="false"></el-table-column>
          <el-table-column label="账号" prop="email"></el-table-column>
          <el-table-column label="密码" prop="password"></el-table-column>
          <el-table-column label="密码加盐" v-if="false" prop="salt"></el-table-column>
          <el-table-column label="联系方式" prop="phone"></el-table-column>
          <el-table-column label="用户名" prop="username"></el-table-column>
          <el-table-column label="创建时间" prop="createTime"></el-table-column>
          <el-table-column label="禁用状态" prop="locked"></el-table-column>
          <el-table-column label="操作" width="300%" style="text-align: center">
            <template slot-scope="scope">
              <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
              <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
              <el-button size="mini" type="warning" @click="handleRest(scope.$index, scope.row)">重置密码</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-main>
      <!-- 分页工具-->
      <el-footer>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[3,5,7]"
          :page-size="PageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="Total">
        </el-pagination>
      </el-footer>
    </el-container>
    <!-- 编辑新增界面-->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="主键" v-if="false">
          <el-input v-model="form.id" ></el-input>
        </el-form-item>
        <el-form-item label="账号：邮箱" >
          <el-input v-model="form.email" ></el-input>
        </el-form-item>
        <el-form-item label="密码" >
          <el-input v-model="form.password" ></el-input>
        </el-form-item>
        <el-form-item label="用户名" >
          <el-input v-model="form.username" ></el-input>
        </el-form-item>
        <el-form-item label="联系方式" >
          <el-input v-model="form.phone" ></el-input>
        </el-form-item>
        <el-form-item label="账号状态" >
          <el-input v-model="form.locked" ></el-input>
        </el-form-item>
        <el-form-item label="创建时间" >
          <el-date-picker v-model="form.createTime" type="date" placeholder="创建时间" style="width: 100%"></el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="concel()">取 消</el-button>
        <el-button type="primary" @click="saveData()">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {
  list,
  remove,
  save,
  getEntity
} from '../../../api/User'
import {
  restPassword
} from '@/api/Login'
import util from '@/util/table.js'

export default {
  data () {
    return {
      author: '',
      tableData: '',
      currentPage: 1,
      PageSize: 5,
      Total: 10,
      currentTotal: 0,
      editState: false,
      dialogFormVisible: false,
      filter: {
        name: '',
        startTime: '',
        endTime: ''
      },
      textMap: {
        update: '编辑',
        create: '添加'
      },
      dialogStatus: '',
      form: {
        id: '',
        email: '',
        password: '',
        username: '',
        phone: '',
        locked: '',
        createTime: ''
      },
      restPassword: '123456'
    }
  },
  methods: {
    handleAdd () {
      this.clean()
      this.editState = false
      this.dialogFormVisible = true
      this.dialogStatus = 'create'
    },
    handleEdit (index, row) {
      this.dialogFormVisible = true
      this.dialogStatus = 'update'
      const para = {
        id: row.id
      }
      getEntity(para).then(res => {
        this.form = res.data
      })
    },
    handleDelete (index, row) {
      this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).then(() => {
        const para = {
          id: row.id
        }
        remove(para).then(res => {
          this.getData()
          if (res.data.successFlg) {
            this.$message({
              message: '操作成功！',
              type: 'success',
              duration: 3 * 1000
            })
          } else {
            this.$message({
              message: '操作失败！错误信息如下：\n' + res.data.errorMsg,
              type: 'error',
              duration: 3 * 1000
            })
          }
          if (this.currentTotal === 1) {
            this.$router.go(0)
          }
        })
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleRest (index, row) {
      const getByid = {
        id: row.id
      }
      getEntity(getByid).then(res => {
        this.form = res.data
      })
      this.$prompt('请输入重置密码', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(({ value }) => {
        if (value === null) {
          value = 12345
        }
        this.form.password = '' + value
        const para = {datajson: this.form}
        restPassword(para).then(res => {
          this.$message({
            type: 'success',
            message: '重置密码为: ' + value
          })
        })
        this.getData()
        if (value === 12345) {
          this.$router.go(0)
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消重置密码'
        })
      })
    },
    getData () {
      this.filter.startTime =
          !this.filter.startTime || this.filter.startTime === ''
            ? null
            : util.formatDate.format(new Date(this.filter.startTime), 'yyyy-MM-dd')

      this.filter.endTime =
          !this.filter.endTime || this.filter.endTime === ''
            ? null
            : util.formatDate.format(new Date(this.filter.endTime), 'yyyy-MM-dd')

      const para = {
        filter: this.filter,
        currentPage: this.currentPage,
        pageSize: this.PageSize
      }

      list(para).then(res => {
        if (res.data.successFlg === true) {
          this.tableData = res.data.data
          this.currentTotal = res.data.currentSize
          this.Total = res.data.total
        } else {
          this.$message({
            message: '操作失败！错误信息如下：\n' + res.data.errorMsg,
            type: 'error',
            duration: 3 * 1000
          })
        }
      }).catch(function (error) {
        this.$message({
          message: '后台未响应' + error,
          type: 'error',
          duration: 3 * 1000
        })
      })
    },
    handleSizeChange (val) {
      this.PageSize = `${val}`
      this.getData()
    },
    handleCurrentChange (val) {
      this.currentPage = `${val}`
      this.getData()
    },
    handleClose (done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {})
    },
    saveData () {
      this.dialogFormVisible = false
      this.form.createTime =
          !this.form.createTime || this.form.createTime === ''
            ? null
            : util.formatDate.format(new Date(this.form.createTime), 'yyyy-MM-dd')

      const para = {
        datajson: this.form
      }

      save(para)
        .then(res => {
          this.getData()
          if (res.data.successFlg === true) {
            this.$message({
              message: '操作成功！',
              type: 'success',
              duration: 3 * 1000
            })
          } else {
            this.$message({
              message: '操作失败！错误信息如下：\n' + res.data.errorMsg,
              type: 'error',
              duration: 3 * 1000
            })
          }
        })
        .catch(function (error) {
          this.$message({
            message: '后台未响应' + error,
            type: 'error',
            duration: 3 * 1000
          })
        })
    },
    concel () {
      this.dialogFormVisible = false
      this.clean()
    },
    clean () {
      this.form.id = ''
      this.form.email = ''
      this.form.password = ''
      this.form.username = ''
      this.form.phone = ''
      this.form.locked = ''
      this.form.createTime = ''
    }
  },
  mounted () {
    // 实现自动加载
    this.getData()
  }
}
</script>

<style scoped>
  .el-header {
    background-color: rgba(169, 235, 36, 0.25);
  }
</style>
